package com.vdong.trade.trading.web.controller;

import com.github.pagehelper.PageInfo;
import com.vdong.trade.trading.common.contract.ApiContract;
import com.vdong.trade.trading.common.enums.TradeDealTypeEnum;
import com.vdong.trade.trading.common.enums.UserTypeEnum;
import com.vdong.trade.trading.common.exception.ProcessException;
import com.vdong.trade.trading.common.foundation.FoundationController;
import com.vdong.trade.trading.entity.po.SettlementTotalParamDTO;
import com.vdong.trade.trading.entity.vo.MerchantDetailVO;
import com.vdong.trade.trading.entity.vo.MerchantRecordVO;
import com.vdong.trade.trading.entity.vo.MerchantTotalListVO;
import com.vdong.trade.trading.entity.vo.PusherSettlementAndCountVO;
import com.vdong.trade.trading.service.SettlementService;
import org.lamb.framework.common.util.CopyUtil;
import org.lamb.framework.common.util.DateUtil;
import org.lamb.framework.common.util.StringUtil;
import org.lamb.framework.core.templete.LambResponseTemplete;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;

import static com.vdong.trade.trading.common.enums.ProcessExceptionEnum.EB00000008;

/**
 * 推客结算
 *
 * @author zhoulongbiao
 * @Date 2018/12/5 18:26
 */
@RestController
@CrossOrigin
public class ReferralSettlementController extends FoundationController {

    @Resource
    private SettlementService settlementService;

    /**
     * 推客结算列表
     */
    @RequestMapping(value = ApiContract.I000000007, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Mono<LambResponseTemplete> settlementTotalList(@RequestBody SettlementTotalParamDTO param) {
        if (StringUtil.hasBlank(param)) return Mono.error(new ProcessException(EB00000008));
        param.setUserType(UserTypeEnum.PUSHER.getValue().toString());
        param.setDealType(TradeDealTypeEnum.ORDER_PUSHER_SUCCESS.getType());
        return returning(CopyUtil.copyObject(settlementService.settlementTotalList(param), new PageInfo<MerchantTotalListVO>()));
    }


    /**
     * 推客结算记录
     */
    @RequestMapping(value = ApiContract.I000000008, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Mono<LambResponseTemplete> settlementRecord(@RequestBody SettlementTotalParamDTO param) {
        if (StringUtil.hasBlank(param)) return Mono.error(new ProcessException(EB00000008));
        /*if (!StringUtil.hasBlank(param.getStartDate(), param.getEndDate())) {
            return returning(CopyUtil.copyObject(settlementService.settlementRecord(SettlementTotalParamDTO.builder().
                    userId(param.getUserId()).startDate(DateUtil.stampToDate(param.getStartDate()))
                    .endDate(DateUtil.stampToDate(param.getEndDate())).build()), new PageInfo<MerchantRecordVO>()));
        }*/
        return returning(CopyUtil.copyObject(settlementService.settlementRecord(param), new PageInfo<MerchantRecordVO>()));
    }

    /**
     * 推客结算详情
     */
    @RequestMapping(value = ApiContract.I000000009, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Mono<LambResponseTemplete> settlementDetail(@RequestBody SettlementTotalParamDTO param) {
        if (StringUtil.hasBlank(param)) return Mono.error(new ProcessException(EB00000008));
        return returning(CopyUtil.copyObject(settlementService.settlementDetail(param), new PageInfo<MerchantDetailVO>()));
    }

    /**
     * 推客总收益 累计已结算  分销数量
     */
    @RequestMapping(value = ApiContract.I000000026, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Mono<LambResponseTemplete> pusherSettlementAndCount(@RequestBody SettlementTotalParamDTO param) {
        if (StringUtil.hasBlank(param)) return Mono.error(new ProcessException(EB00000008));
        return returning(CopyUtil.copyObject(settlementService.pusherSettlementAndCount(param), new PusherSettlementAndCountVO()));
    }
}
